package com.corpgovernment.travelstandard.convert;


import cn.hutool.core.util.ObjectUtil;
import com.corpgovernment.api.travelstandard.vo.MbTravelstandHotelQuarterVo;
import com.corpgovernment.travelstandard.entity.db.MbTravelstandHotelQuarter;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author jhxue
 * @create 2022-05-12-18:05
 */
@Component
public class HotelStandardConvert {

	public static MbTravelstandHotelQuarterVo convertToTravelStandardHotel(MbTravelstandHotelQuarter mbTravelstandHotelQuarter) {
		if (mbTravelstandHotelQuarter == null) {
			return null;
		}
		MbTravelstandHotelQuarterVo target = new MbTravelstandHotelQuarterVo();
		BeanUtils.copyProperties(mbTravelstandHotelQuarter, target);
		if(ObjectUtil.isNotNull(mbTravelstandHotelQuarter.getStartTime()) && ObjectUtil.isNotNull(mbTravelstandHotelQuarter.getEndTime())){
			Map<String, Date> res = getDateMap(mbTravelstandHotelQuarter.getStartTime(),mbTravelstandHotelQuarter.getEndTime());
			target.setBeginDate(res.get("startDate"));
			target.setEndDate(res.get("endDate"));
		}else {
			target.setBeginDate(null);
			target.setEndDate(null);
		}
		return target;
	}

	public static Map<String,Date> getDateMap(String start, String end) {

		Map<String, Date> stringMap = new HashMap();
        Calendar startCal = null;
        Calendar endCal = null;
        SimpleDateFormat df = new SimpleDateFormat("MM-dd");
        df.setLenient(true);
        if(StringUtils.isNotBlank(start)){
			String[] splits = start.split("-");
			if(splits.length < 2){
				stringMap.put("startDate",null);
			}else {
				int tagDay = Integer.parseInt(start.split("-")[1]);
				int tagMonth = Integer.parseInt(start.split("-")[0]);
				startCal = Calendar.getInstance();
				int year = startCal.get(Calendar.YEAR);
				startCal.clear();
				startCal.set(year,tagMonth-1,tagDay);
				int dayOfMonth = startCal.get(Calendar.DAY_OF_MONTH);
				if(dayOfMonth != tagDay){
					startCal.set(year, tagMonth-1, 1);
					int maxDay = startCal.getActualMaximum(Calendar.DAY_OF_MONTH); // 获取该月的最大天数
					startCal.set(year, tagMonth-1, maxDay);
				}
				stringMap.put("startDate",startCal.getTime());
			}
        }
        if(StringUtils.isNotBlank(end)){
			String[] splits = end.split("-");
			if(splits.length < 2){
				stringMap.put("endDate",null);
			}else {
				int tagDay = Integer.parseInt(end.split("-")[1]);
				int tagMonth = Integer.parseInt(end.split("-")[0]);
				endCal = Calendar.getInstance();
				int year = endCal.get(Calendar.YEAR);
				endCal.set(year,tagMonth-1,tagDay, 23,59,59);
				int dayOfMonth = endCal.get(Calendar.DAY_OF_MONTH);
				if(dayOfMonth != tagDay){
					endCal.set(year, tagMonth-1, 1);
					int maxDay = endCal.getActualMaximum(Calendar.DAY_OF_MONTH); // 获取该月的最大天数
					endCal.set(year, tagMonth-1, maxDay, 23,59,59);
				}
				if(startCal!= null && startCal.compareTo(endCal) > 0){
					if(startCal.getTime().compareTo(new Date()) > 0){
						startCal.set(Calendar.YEAR, startCal.get(Calendar.YEAR) -1);
						stringMap.put("startDate",startCal.getTime());
					}else {
						year = year + 1;
						endCal.set(year,tagMonth-1,tagDay, 23,59,59);
					}
				}
				stringMap.put("endDate",endCal.getTime());
			}

        }
		return stringMap;
	}

    public static void main(String[] args) {

		// 验证 01-05 -- 04-04  +  07-05 -- 04-04  +  04-04 -- 07-05
		String s = "01-05";
		String e = "01-04";
		Map rs= getDateMap(s,e);
		System.out.println(rs); // 输出：2022-07-31
		System.out.println("=====================");
		Calendar startCal = Calendar.getInstance();
		int year = startCal.get(Calendar.YEAR);
		startCal.set(year,3,12, 0, 0,0);
		System.out.println(startCal.getTime().getTime());
	}


	/**
	 * 将模型转换成DO
	 */
	public static List<MbTravelstandHotelQuarterVo> convertToTravelStandardHotelList(List<MbTravelstandHotelQuarter> daoList) {
		List<MbTravelstandHotelQuarterVo> res = new ArrayList<>();
		if (CollectionUtils.isEmpty(daoList)) {
			return Collections.EMPTY_LIST;
		}
		for (int i = 0; i < daoList.size(); i++) {
			MbTravelstandHotelQuarterVo item = convertToTravelStandardHotel(daoList.get(i));
			if(ObjectUtil.isNotNull(item)){
				res.add(item);
			}

		}
		return res;
	}

}
